/*
 * @Author: zhangfeng
 * @Date: 2023-12-27 13:34:19
 * @LastEditTime: 2024-01-09 17:29:14
 * @LastEditors: zhangfeng
 * @Description
 * @FilePath: \manager\managersystem\src\composables\useRoles.ts
 * © 2023 张峰，All rights reserved.
 */

import {
	getRoleAll,
	createRole,
	updateRole,
	deleteRole,
	getPremissionAllByRoleId,
} from '../api/role.ts';

export function useRoles() {
	const loading = ref(true);
	const roleTableData = ref([] as RoleTableData[]);
	// 获取角色
	const getAllRoles = async () => {
		loading.value = true;
		const res: AxiosResponse = await getRoleAll();
		roleTableData.value = res.result;
		loading.value = false;
	};
	// 添加角色
	const createRoles = async (data: RoleQuery) => {
		const res = await createRole(data);
		return res;
	};
	// 修改角色
	const updateRoles = async (data: RoleQuery) => {
		const res = await updateRole(data);
		return res;
	};
	// 删除角色
	const deleteRoles = async (id: number) => {
		const res = await deleteRole(id);
		return res;
	};

	// 根据角色获取权限

	const getPremissionAll = async (roleId: number) => {
		const res: AxiosResponse<ResponseData> =
			await getPremissionAllByRoleId(roleId);
		console.log(res);
		return res;
	};

	return {
		loading,
		getAllRoles,
		roleTableData,
		createRoles,
		updateRoles,
		deleteRoles,
		getPremissionAll,
	};
}
